#import "HelloWorldApp.h"

@implementation HelloWorldApp
- (void) dealloc{
    //release all
    [window release];
    [mainView release];
    [table release];
    [navbar release];
    [list release];
    [super dealloc];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero] autorelease];
	//static const char *unit[] = {"Maps","Routes","BookMark"}; 
	// cell.text = [list ]
	NSInteger row = [indexPath row];
	NSInteger section = [indexPath section];
    cell.text = [[list objectAtIndex: section] objectAtIndex: row];
/*
	CGRect tRect = CGRectMake(0.0f,5.0f,320.0f,40.0f);
	id detail = [[UILable alloc] initWithFrame:tRect];
	[detail setText: unit[section]];
	[cell addSubView: detail];
*/	
    return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return [[NSArray arrayWithObjects:@"Maps",@"Routes",@"BookMark",nil] objectAtIndex: section]; 
}

- (NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
    return [[list objectAtIndex:section] count];
}

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
	
	return [list count];
}


- (void) applicationDidFinishLaunching: (id) unused
{

    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];


    /* Create a main view */
    mainView = [[UIView alloc] initWithFrame: [[UIScreen mainScreen] bounds]];


    navbar =  [[UINavigationBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 60.0f)];
    [navbar setDelegate: self];
    [navbar pushNavigationItem:[[UINavigationItem alloc] initWithTitle: @"OfflineMaps"] animated : true];
    
    [mainView addSubview:navbar];
	list = [[NSMutableArray alloc] init];
	for ( int i = 0; i<3 ; i++) [list addObject:[[NSMutableArray alloc] init]];
	// 0 => Maps
	// 1 => Routes
	// 2 => BookMarks
	
	NSMutableArray *maps = [[NSMutableArray alloc] init];
    
    NSFileManager *manager = [NSFileManager defaultManager];
    [maps addObjectsFromArray:[manager contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@/Media/Maps", NSHomeDirectory()] error:NULL]];
    for (NSString *path in maps) {
        //check if list is a folder...
		BOOL isDir;
		if ([manager fileExistsAtPath: [NSString stringWithFormat:@"/var/mobile/Media/Maps/%@",path] isDirectory: &isDir ] && isDir){
			// if path is a folder
			NSString *folder = [NSString stringWithFormat:@"/var/mobile/Media/Maps/%@",path];
			if([manager fileExistsAtPath:[NSString stringWithFormat:@"%@/MapTiles.sqlitedb",folder]]){
			// if this folder content MapTiles.sqlitedb file , this folder must be a Map folder...
			
				[[list objectAtIndex: 0 ] addObject: path];
			}
		}
		else
			[[list objectAtIndex: 1] addObject: path];
		
    }

	table = [[UITableView alloc] initWithFrame:CGRectMake(0.0f,60.0f,320.0f,420.0f) style: 1];
	// 1 means UITableViewStyleGrouped 
	[mainView addSubview: table];
	[table setDataSource:self];
    [table setDelegate:self];

    [table setEditing:NO animated:NO];
    table.allowsSelectionDuringEditing = YES;

    [table setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
    /* Setup window */
    [window makeKeyAndVisible];
    [window addSubview: mainView];
}
@end
